﻿// <copyright file="DomainViewOwned.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Controls;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class DomainViewOwner
    {
        partial void DomainViewOwner_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            this.DisplayName = string.Format("View {0} details", this.Domain.Name);

            List<DomainProvince> provinces = new List<DomainProvince>();

            // Get all the Provinces owned by the Domain or where the Domain has a Holding
            provinces.AddRange(this.Provinces);
            provinces.AddRange(this.LawHoldings.Select(h => h.Province));
            provinces.AddRange(this.TempleHoldings.Select(h => h.Province));
            provinces.AddRange(this.GuildHoldings.Select(h => h.Province));
            provinces.AddRange(this.SourceHoldings.Select(h => h.Province));

            List<Domain> allRegents = new List<Domain>();
            foreach (var province in provinces.Distinct())
            {
                allRegents.AddRange(province.LawHoldings
                    .Where(h => h.Visibility == (int)VisibilityEnum.Public)
                    .Select(h => h.Owner));

                allRegents.AddRange(province.TempleHoldings
                    .Where(h => h.Visibility == (int)VisibilityEnum.Public)
                    .Select(h => h.Owner));

                allRegents.AddRange(province.GuildHoldings
                    .Where(h => h.Visibility == (int)VisibilityEnum.Public)
                    .Select(h => h.Owner));

                allRegents.AddRange(province.SourceHoldings
                    .Where(h => h.Visibility == (int)VisibilityEnum.Public)
                    .Select(h => h.Owner));
            }

            this.Regents = string.Concat(allRegents
                .Distinct()
                .Where(d => d != null)
                .OrderBy(d => d.Name)
                .Select(d => string.Format("{0} ({1}), ", d.Name, d.Abbreviation)));

            if (this.Regents.Length > 0)
            {
                this.Regents = this.Regents.Remove(this.Regents.Length - 2, 2);
            }
        }

        #region Edit DomainProvince

        partial void ProvincesEditSelected_CanExecute(ref bool result)
        {
            result = this.Provinces.SelectedItem != null;
        }

        partial void ProvincesEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowProvince");
            this.FindControl("ModalWindowProvince").DisplayName = string.Format("Edit {0} details", this.Provinces.SelectedItem.Name);
            this.FindControl("ModalWindowProvince").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowProvince_Execute()
        {
            this.CloseModalWindow("ModalWindowProvince");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowProvince_Execute()
        {
            this.CloseModalWindow("ModalWindowProvince");
            this.Provinces.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Edit LawHolding

        partial void LawHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.LawHoldings.SelectedItem != null;
        }

        partial void LawHoldingsEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowLaw");
            this.FindControl("ModalWindowLaw").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowLaw_Execute()
        {
            this.CloseModalWindow("ModalWindowLaw");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowLaw_Execute()
        {
            this.CloseModalWindow("ModalWindowLaw");
            this.LawHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Edit Temple Holding

        partial void TempleHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.TempleHoldings.SelectedItem != null;
        }

        partial void TempleHoldingsEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowTemple");
            this.FindControl("ModalWindowTemple").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowTemple_Execute()
        {
            this.CloseModalWindow("ModalWindowTemple");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowTemple_Execute()
        {
            this.CloseModalWindow("ModalWindowTemple");
            this.TempleHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Edit GuildHolding

        partial void GuildHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.GuildHoldings.SelectedItem != null;
        }

        partial void GuildHoldingsEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowGuild");
            this.FindControl("ModalWindowGuild").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowGuild_Execute()
        {
            this.CloseModalWindow("ModalWindowGuild");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowGuild_Execute()
        {
            this.CloseModalWindow("ModalWindowGuild");
            this.GuildHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Edit SourceHolding

        partial void SourceHoldingsEditSelected_CanExecute(ref bool result)
        {
            result = this.SourceHoldings.SelectedItem != null;
        }

        partial void SourceHoldingsEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowSource");
            this.FindControl("ModalWindowSource").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowSource_Execute()
        {
            this.CloseModalWindow("ModalWindowSource");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowSource_Execute()
        {
            this.CloseModalWindow("ModalWindowSource");
            this.SourceHoldings.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Edit TradeRoute

        partial void TradeRoutesOwnedByDomainEditSelected_CanExecute(ref bool result)
        {
            result = this.TradeRoutesOwnedByDomain.SelectedItem != null;
        }

        partial void TradeRoutesOwnedByDomainEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowTradeRoute");
            this.FindControl("ModalWindowTradeRoute").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRoute");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowTradeRoute_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRoute");
            this.TradeRoutesOwnedByDomain.SelectedItem.Details.DiscardChanges();
        }
        
        #endregion

        #region Edit LeyLine

        partial void LeyLinesFromDomainEditSelected_CanExecute(ref bool result)
        {
            result = this.LeyLinesOwnedByDomain.SelectedItem != null;
        }

        partial void LeyLinesFromDomainEditSelected_Execute()
        {
            this.OpenModalWindow("ModalWindowLeyLine");
            this.FindControl("ModalWindowLeyLine").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLine");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowLeyLine_Execute()
        {
            this.CloseModalWindow("ModalWindowLeyLine");
            this.LeyLinesOwnedByDomain.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region View TradeRoute (Province)

        partial void ViewTradeRouteFromProvinceDetails_CanExecute(ref bool result)
        {
            result = this.TradeRoutesInDomainProvince.SelectedItem != null &&
                     this.TradeRoutesInDomainProvince.SelectedItem.HasPublicInformation == true;
        }

        partial void ViewTradeRouteFromProvinceDetails_Execute()
        {
            this.OpenModalWindow("ModalWindowTradeRouteFromProvince");
            this.FindControl("ModalWindowTradeRouteFromProvince").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowTradeRouteFromProvince_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteFromProvince");
        }

        #endregion

        #region View TradeRoute (Guild)

        partial void ViewTradeRoutesFromGuildDetails_CanExecute(ref bool result)
        {
            result = this.TradeRoutesInDomainProvinceByGuild.SelectedItem != null &&
                     this.TradeRoutesInDomainProvinceByGuild.SelectedItem.HasPublicInformation == true;
        }

        partial void ViewTradeRoutesFromGuildDetails_Execute()
        {
            this.OpenModalWindow("ModalWindowTradeRouteFromGuild");
            this.FindControl("ModalWindowTradeRouteFromGuild").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(ModalWindow_ControlAvailable);
        }

        partial void SubmitModalWindowTradeRouteFromGuild_Execute()
        {
            this.CloseModalWindow("ModalWindowTradeRouteFromGuild");
        }

        #endregion

        #region Helper Methods

        private void ModalWindow_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            ChildWindow window = (ChildWindow)e.Control;
            window.Closed += new EventHandler(window_Closed);
        }

        private void window_Closed(object sender, EventArgs e)
        {
            ChildWindow window = (ChildWindow)sender;
            if (!(window.DialogResult.HasValue && window.DialogResult.Value))
            {
                window.Closed -= window_Closed;
            }
        }

        #endregion
    }
}